package com.example.myapplication.hospital;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.InquirycaseBean;
import com.example.myapplication.ShuJu.MyconsultationtalBean;
import com.example.myapplication.ShuJu.PetclassificationBean;
import com.example.myapplication.apter.InquirycaseApter;
import com.example.myapplication.apter.MyconsultationtalApter;
import com.example.myapplication.apter.PetclassificationApter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PethospitalActivity extends AppCompatActivity {

    private ImageView bloak;
    private RecyclerView resort;
    private TabLayout tab_pethospital;
    private RecyclerView re_pethospital;
    private RecyclerView re_myconsultation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pethospital);
        initview();
        //返回
        but_bloak();
        //宠物分类
        petclassification();
        Inquirycase();
        //问诊列表和问诊案例列表
        pethospitalsort();
    }

    private void pethospitalsort() {
        ArrayList<String> list = new ArrayList<>();
        list.add("问诊案例");
        list.add("我的问诊");
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tab_pethospital.newTab();
            tab.setText(list.get(i));
            tab_pethospital.addTab(tab);
        }
        sortlist();
    }

    private void sortlist() {
        tab_pethospital.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String Name = tab.getText().toString();
                int position = tab.getPosition();
                if ("问诊案例".equals(Name)) {
                   Inquirycase();
                } else if ("我的问诊".equals(Name)) {
                    myconsultation();
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    //问诊案例
    private void Inquirycase() {
        re_pethospital.setVisibility(View.VISIBLE);
        re_myconsultation.setVisibility(View.GONE);
        OKHttpTool.doNetRequest("prod-api/api/pet-hospital/inquiry/list?pageNum=1&pageSize=10",
                OKHttpTool.GET,
                InquirycaseBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                InquirycaseBean inquirycaseBean = (InquirycaseBean) obj;
                                if (inquirycaseBean.getCode() == 200) {
                                    re_pethospital.setLayoutManager(new LinearLayoutManager(PethospitalActivity.this));
                                    re_pethospital.setAdapter(new InquirycaseApter(PethospitalActivity.this, inquirycaseBean.getRows()));

                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    //我的问诊
    private void myconsultation() {
        re_pethospital.setVisibility(View.GONE);
        re_myconsultation.setVisibility(View.VISIBLE);
        OKHttpTool.doNetRequest("prod-api/api/pet-hospital/inquiry/my-list",
                OKHttpTool.GET,
                MyconsultationtalBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                MyconsultationtalBean myconsultationtalBean = (MyconsultationtalBean) obj;
                                if (myconsultationtalBean.getCode() == 200) {
                                    re_myconsultation.setLayoutManager(new LinearLayoutManager(PethospitalActivity.this));
                                    re_myconsultation.setAdapter(new MyconsultationtalApter(PethospitalActivity.this, myconsultationtalBean.getRows()));
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });

    }

    private void petclassification() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(TokenBean.IP + "/prod-api/api/pet-hospital/pet-type/list")
                .addHeader("Authorization", OKHttpTool.TOKEN)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String JSON = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        PetclassificationBean petclassificationBean = gson.fromJson(JSON, PetclassificationBean.class);
                        if (petclassificationBean.getCode() == 200) {
                            resort.setLayoutManager(new GridLayoutManager(PethospitalActivity.this, 5));
                            resort.setAdapter(new PetclassificationApter(PethospitalActivity.this, petclassificationBean.getData()));
                        }
                    }
                });
            }
        });

    }

    private void but_bloak() {
        bloak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PethospitalActivity.this.finish();
            }
        });
    }

    private void initview() {
        bloak = findViewById(R.id.bloak);
        resort = findViewById(R.id.resort);
        tab_pethospital = findViewById(R.id.tab_pethospital);
        re_pethospital = findViewById(R.id.re_pethospital);
        re_myconsultation = findViewById(R.id.re_myconsultation);
    }
}